<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>
<script>
import axios from 'axios'
import { getProvinceMapInfo } from '@/utils/map_utils'
export default {
  data () {
    return {
      echartInstance: null,
      allData: null,
      mapData: [] // 获取的省份地图矢量数据
    }
  },
  mounted () {
    this.initCharts()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destoryed () {
    window.removeEventListener('resize', this.screenAdapter())
  },
  methods: {
    // 初始化图表
    async initCharts () {
      this.echartInstance = this.$echarts.init(this.$refs.map_ref, 'chalk')
      // 因为地图放在本地，而不是从后台取，路径不同需要单独写
      // http://localhost:8999/static/map/china.json
      const { data: res } = await axios.get('http://localhost:8999/static/map/china.json')
      // 注册地图
      this.$echarts.registerMap('china', res)
      const initOption = {
        title: {
          text: '▌ 商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2172bf',
            borderColor: '#333'
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      this.echartInstance.setOption(initOption)
      this.echartInstance.on('click', async arg => {
        //  arg.name 得到点击的省份，但是是中文
        // console.log(arg)
        // 中文转拼音
        const provinceInfo = getProvinceMapInfo(arg.name)
        console.log(provinceInfo)
        if (!this.mapData[provinceInfo.key]) {
          // 获取省份地图数据
          const { data: res } = await axios.get('http://localhost:8999' + provinceInfo.path)
          // 存到mapData 中
          this.mapData[provinceInfo.key] = res
          // 注册点击的省份地图
          this.$echarts.registerMap(provinceInfo.key, res)
        }
        const changeOption = {
          geo: {
            map: provinceInfo.key
          }
        }
        this.echartInstance.setOption(changeOption)
      })
    },
    // 发送ajax请求
    async getData () {
      // 获取后台的散点数据
      const { data: res } = await this.$http.get('map')
      this.allData = res
      this.updateCharts()
    },
    // 更新图表
    updateCharts () {
      // 图例数据
      const legendArr = this.allData.map(item => {
        return item.name
      })
      const seriesArr = this.allData.map(item => {
        // 如果想在地图中显示散点数据，需要给散点的图表添加配置： coordinateSystem: geo
        // 一个类别下的所有散点数据
        return {
          type: 'effectScatter',
          // 涟漪效果
          rippleEffect: {
            scale: 5,
            brushType: 'stroke'
          },
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo'
        }
      })
      const updateOption = {
        legend: {
          data: legendArr
        },
        series: seriesArr
      }
      this.echartInstance.setOption(updateOption)
    },
    // 事件监听
    screenAdapter () {
      const titleFontSize = this.$refs.map_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2
          }
        }
      }
      this.echartInstance.setOption(adapterOption)
      this.echartInstance.resize()
    },
    // 返回中国地图 配置geo即可
    revertMap () {
      const revertOption = {
        geo: {
          map: 'china'
        }
      }
      this.echartInstance.setOption(revertOption)
    }
  }
}
</script>
